﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Infrastructure;
using System.Timers;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
using DBDataAccessLib.DataMapping;
namespace UserControlLib
{
    /// <summary>
    /// Interaction logic for PlayInfoWindow.xaml
    /// </summary>
    public partial class PlayInfoWindow : Window, INotifyPropertyChanged
    {
        private bool needCloseWindow = false;
        private string option = Helper.GetConfigurationValue("DisplayOptions");
        private int tick = 0;
        private System.Timers.Timer timer;
        private string wcno;
        private string stationName;
        private SynchronizationContext context;
        public PlayInfoWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            if (option == "AutoPlay")
            {
                timer = new System.Timers.Timer();
                timer.Elapsed += new ElapsedEventHandler((p, ev) =>
                 {
                     ShowNextImage(1);
                 });
                //timer.Interval = int.Parse(Helper.GetConfigurationValue("AutoPlayInterval"));
            }
            this.Closing += new System.ComponentModel.CancelEventHandler(PlayInfoWindow_Closing);
            //wcno = Helper.GetConfigurationValue("WCNO");
            //stationName = Helper.GetConfigurationValue("StationName");
            context = SynchronizationContext.Current;
        }
        void PlayInfoWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!needCloseWindow)
            {
                this.Visibility = Visibility.Hidden;
                e.Cancel = true;
                if (option == "AutoPlay" && timer != null) timer.Stop();
            }
        }

        private MachineInfoRecordMapping record;
        public MachineInfoRecordMapping Record
        {
            get
            {
                return record;
            }
            set
            {
                record = value;
                wcno = value.WCNO;
                stationName = value.StationName;
                this.DisplayInfo = "流水线名：" + value.WCNO + "；工位名称：" + value.StationName + "；机种名：" + value.Model + "；背番号：" +
                    value.Sebango + "；使用状态：" + GetValid(value.Valid) + "；图片名：";
            }
        }

        private string GetValid(int valid)
        {
            if (valid == 0) return "预备";
            if (valid == 1) return "现行";
            return "旧数据";
        }

        public void CloseWindow()
        {
            needCloseWindow = true;
            if (timer != null)
            {
                timer.Stop();
                timer.Dispose();
            }
            this.Close();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape) this.Close();
            if (option != "AutoPlay")
            {
                if (e.Key == Key.Left) ShowNextImage(-1);
                else if (e.Key == Key.Right) ShowNextImage(1);
            }
        }

        private string displayInfo;

        public string DisplayInfo
        {
            get { return displayInfo; }
            set
            {
                displayInfo = value;
                OnPropertyChanged("DisplayInfo");
            }
        }

        private string imageInfo;

        public string ImageInfo
        {
            get { return imageInfo; }
            set
            {
                imageInfo = value;
                OnPropertyChanged("ImageInfo");
            }
        }


        private string selectedImage;

        public string SelectedImage
        {
            get { return selectedImage; }
            set
            {
                if (selectedImage != value)
                {
                    selectedImage = value;
                    OnPropertyChanged("SelectedImage");
                }
            }
        }

        private void ShowNextImage(int sequence)
        {
            context.Send(p =>
            {
                if (sequence < 0)
                {
                    if (tick == int.MinValue) tick = 1;
                    tick--;
                }
                else
                {
                    if (tick == int.MinValue) tick = -1;
                    tick++;
                }
                if (tick < 0) tick = Record.Essentials.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Length - 1;
                else if (tick == Record.Essentials.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Length) tick = 0;
                string baseUri = Helper.GetConfigurationValue("Client") == "Office" ? Helper.ServerUrl : Helper.BaseUri;
                ImageInfo = Record.Essentials.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries)[tick];
                SelectedImage = baseUri + wcno + "\\" + stationName + "\\" + ImageInfo;
            }, null);
        }

        public void Reset()
        {
            tick = int.MinValue;
            ShowNextImage(1);
            if (timer != null && Record != null)
            {
                timer.Interval = int.Parse(Record.CycleTime) * 1000;
                timer.Start();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
